package java_thinking.unit_18_IO._18_6;

import java.io.*;

/**
 * @Desc:   存储和恢复数据
 *          1.DataInputStream / DataOutputStream 都是面向字节的，以固定格式读写相关内容
 *          2.如果我们使用 DataOutputStream 写入数据，java保证我们可以使用 DataInputStream 准确的读取数据，无论读写数据的平台多不同
 *          3.UTF-8是一种多字节格式，其编码长度根据实际使用的字符集会有所变化，UTF-8将ASCII字符编码成单一字节的形式，
 *            而非ASCII字符则编码成两到三个字节的形式，另外字符串的长度存储在UTF-8字符串的前两个字节中，但是writeUTF()
 *            和readUTF()用的是适用于javaUTF-8变体，所以在读取非java程序写的字符串时要进行一些特殊编码
 *          4.UTF-8 是 Unicode 的实现方式之一，UTF-8 最大的一个特点，就是它是一种变长的编码方式。它可以使用1~4个字节表示一个
 *            符号，根据不同的符号而变化字节长度
 * @author: cww
 * @DateTime: 2019/5/14 20:37
 */

public class StoringAndRecoveringData {
    static String file = "src/maindemo/java/java_thinking/unit_18_IO/_18_6/StoringAndRecoveringData.txt";
    public static void main(String[] args) throws IOException {
        DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
        out.writeDouble(3.141592653);
        out.writeUTF("what is Chinese 中国");
        out.writeDouble(1.141592653);
        out.writeUTF("what is English");
        out.writeBoolean(true);
        out.writeByte(100);
        out.writeByte(255);
        out.writeChar('A');
        out.writeFloat(1.41413f);
        out.writeLong(1000000000L);
        out.writeInt(100000);
        out.writeShort(30000);
        out.writeShort(65535);
        out.writeDouble(3.14159);
        out.writeUTF("That was pi");
        out.close();
        DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
        System.out.println(in.readDouble());
        System.out.println(in.readUTF());
        System.out.println(in.readDouble());
        System.out.println(in.readUTF());
        System.out.println(in.readBoolean());
        System.out.println(in.readByte());
        System.out.println(in.readUnsignedByte());
        System.out.println(in.readChar());
        System.out.println(in.readFloat());
        System.out.println(in.readLong());
        System.out.println(in.readInt());
        System.out.println(in.readShort());
        System.out.println(in.readUnsignedShort());
        System.out.println(in.readDouble());
        System.out.println(in.readUTF());
        in.close();
    }
}
